<?php
/**
 * User_Variable
 *
 * Класс для работы с древовидной структурой
 *
 */
class User_Variable{
	/**
	 * получение значений переменных рользователя
	 *
	 * @param array|int $arrUserIDs - массив или один ID_user
	 * @param array|string $arrValuesName - массив или одно valueName
	 */
	public static function getVariableValue($idUser, $variableName, $simpleArray = false) {
		$objDB = Service_Database::instance();
		$stmtWhere = ' AND `user_variable`.`variable_name` = "' . $variableName . '"';
		$stmtWhat = 'SELECT `user_variable`.`variable_value`';
		if (is_array($variableName)) {
			$stmtWhat = 'SELECT `user_variable`.`variable_value`, `user_variable`.`variable_name` ';
			$stmtWhere = ' AND `user_variable`.`variable_name` IN ("' . join('", "', $variableName) . '")';
		}
		if ($idUser) {
			$stmt = $stmtWhat . 'FROM `user_variable`
						 WHERE `user_variable`.`ID_user` = ' . $idUser . $stmtWhere;
				if (is_array($variableName)) {
					$arrSelect = $objDB->select($stmt);
					if (!$simpleArray)
      					return $arrSelect;
     				else {
     					$arrReturn = array();
      					foreach ($arrSelect as $arrSelect)
      						 $arrReturn[$arrSelect['variable_name']] = $arrSelect['variable_value'];
      					return $arrReturn;
     					}
				}
				else {
					return $objDB->selectSimpleField($stmt);
				}
		}
		else {
			$objSession = User_Session::instance();
			if (is_array($variableName)) {
				$arrResult = $objSession->__getArray();
				if ($simpleArray)
					return $arrResult;
				else {
					$i = 0;
					$arrReturn = array();
					if (sizeof($arrResult) > 0)
						foreach ($arrResult as $key => $value) {
							$arrReturn[$i]['variable_name'] = $key;
							$arrReturn[$i]['variable_value'] = $value;
							$i++;
						}
					return $arrReturn;
				}
			}
			return $objSession->__get($variableName);
		}

	}

	public static function addVariableValue($idUser, $variableName, $variableValue) {
		$objDB = Service_Database::instance();
/*		switch ($variableName) {
			case "tree_structure_linear_flag" : true; break;
			default: return false;
		}*/

		if (is_array($variableValue) && is_array($variableName)) {
			$arrVarNameSize = sizeof($variableName);
			$arrVarValueSize = sizeof($variableValue);
			if ($arrVarNameSize == $arrVarValueSize) {
				for($i=0; $i < $arrVarNameSize; $i++) {
					self::addVariableValue($idUser, $variableName[$i], $variableValue[$i]);
				}
				return true;
			}
			return false;
		}
		if ($idUser) {
			if ($objDB->isRecordExist('SELECT `user_variable`.`variable_value` FROM `user_variable` WHERE `user_variable`.`ID_user` = ' . $idUser . ' AND `user_variable`.`variable_name` = "' . $variableName . '"')) {
				$arrUpdate = array('variable_value' => $variableValue);
				$arrWhere = array('ID_user' => $idUser, 'variable_name' => $variableName);
				$objDB->update('user_variable', $arrUpdate, $arrWhere);
			}
			else {
				$arrInsert = array('ID_user' => $idUser, 'variable_name' => $variableName, 'variable_value' => $variableValue);
				$objDB->insert('user_variable', $arrInsert);
			}
			return true;
		}
		else {
		if (is_array($variableValue) && is_array($variableName)) {
			$arrVarNameSize = sizeof($variableName);
			$arrVarValueSize = sizeof($variableValue);
			if ($arrVarNameSize == $arrVarValueSize) {
				$arrAdd = array();
				for($i=0; $i < $arrVarNameSize; $i++) {
					$arrAdd[$variableName[$i]] = $variableValue[$i];
				}
				$objSession->__setArray($arrAdd);
				return true;
			}
			return false;
		}
			$objSession = User_Session::instance();
			$objSession->__set($variableName, $variableValue);
			return true;
		}
		return false;
	}
}
?>